<?php

namespace plugin\ycMassage\utils;

use \Exception;
use plugin\ycMassage\utils\enum\CouponUseType;

class ServicePriceBuilder
{
    # 服务总原价
    public $origin_price = 0;
    # 服务总价
    public $price = 0;
    # 服务总时长
    public $service_duration = 0;
    # 基础服务总价
    public $basic_price = 0;
    # 基础服务总时长
    public $basic_duration = 0;
    # 加钟服务总价
    public $overtime_price = 0;
    # 加钟服务总时长
    public $overtime_duration = 0;
    # 物料服务总价
    public $material_price = 0;
    # 总服务数量
    public $num = 0;
    # 总基础服务数量
    public $basic_num = 0;
    # 总加钟服务数量
    public $overtime_num = 0;
    # 优惠券总抵扣
    public $coupon_money = 0;
    # 优惠券平均抵扣
    public $coupon_average_money = 0;
    # 服务订单
    public $order = [];
    # 服务标题组
    public $title = [];
    # 使用的优惠券
    public $coupon = [];
    public function __construct($model)
    {
        foreach ($model as $item) {
            $num = $item->num;
            $this->num += $num;
            $p = 0;
            $duration = 0;
            $this->material_price += $item->material_price;
            $this->price += $item->price;
            $this->origin_price += $item->price;
            $this->service_duration += $item->service_duration;
            $this->basic_price += $item->price;
            $this->basic_duration += $item->service_duration;
            $this->basic_num++;
            $num--;
            $this->overtime_num += $num;
            if ($num > 0) {
                if ($item->overtime_separate) {
                    $p = $item->overtime_price * $num;
                    $duration = $item->overtime_duration * $num;
                } else {
                    $p = $item->price * $num;
                    $duration = $item->service_duration * $num;
                }
            }
            if ($p) {
                $this->overtime_price += $p;
                $this->overtime_duration += $duration;
                $this->price += $p;
                $this->origin_price += $p;
                $this->service_duration += $duration;
            }
            $this->order[] = [
                'service_id' => $item->service_id,
                'num' => $item->num,
                'origin_price' => $item->price + $p,
                'price' => $item->price + $p,
                'unit_price' => $item->price,
                'material_price' => $item->material_price,
                'overtime_price' => $p,
                'duration' => $item->service_duration + $duration,
                'overtime_duration' => $duration,
                'overtime_num' => $num
            ];
            $this->title[] = $item->title;
        }
    }
    public function useCoupon($coupons)
    {
        foreach ($coupons as $item) {
            switch ($item->use_type) {
                case CouponUseType::DEC['value']:
                    $this->coupon_money += $item->money;
                    $this->coupon[] = $item->id;
                    break;
                case CouponUseType::FULL_DEC['value']:
                    if ($this->price >= $item->full_money) {
                        $this->coupon_money += $item->money;
                        $this->coupon[] = $item->id;
                    }
                    break;
            }
        }
        if ($this->coupon_money > 0) {
            $this->coupon_average_money = round($this->coupon_money / $this->num, 2);
            if ($this->coupon_average_money > 0) {
                foreach ($this->order as &$item) {
                    $coupon_money = $this->coupon_average_money * $item['num'];
                    $item['coupon_money'] = $coupon_money;
                    $item['price'] = $item['price'] - $coupon_money;
                }
                $this->price -= $this->coupon_money;
            }
        }
    }
}
